<template>
  <!-- 新增员工弹出层 -->
  <el-dialog title="新增员工" :visible="showDialog" @close="cancel">
    <el-form
      :model="addForm"
      style="margin:0px auto;width:500px"
      label-width="100px"
      :rules="rules"
      ref="addForm"
    >
      <el-form-item label="姓名" prop="username">
        <el-input v-model="addForm.username"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="addForm.mobile"></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="addForm.timeOfEntry"
          type="date"
          placeholder="请选择日期"
          style="width:400px"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="addForm.formOfEmployment"
          placeholder="请选择"
          style="width:400px"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="addForm.workNumber"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="addForm.departmentName"
          @focus="showTreeData"
          @blur="OnBlur"
        ></el-input>
        <el-tree
          v-if="showTree"
          :data="treeData"
          :props="{ label: 'name' }"
          :default-expand-all="true"
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="addForm.correctionTime"
          type="date"
          placeholder="请选择日期"
          style="width:400px"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="isOk">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 导入接口
import { addEmp } from '@/api/employees'
// 导入获取部门列表接口
import { getDepartments } from '@/api/departments'
// 导入处理树状数组的工具
import { transToTreeData } from '@/utils/index'
// 导入枚举库
import constant from '@/api/constant/employees'

export default {
  name: 'addEmp',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 表单绑定的数据
      addForm: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      // 枚举库
      hireType: constant.hireType,
      treeData: [], // 定义数组接收树形数据
      showTree: false, // 控制树形的显示或者隐藏
      // 效验规则
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 4,
            message: '用户姓名为1-4位'
          }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 部门输入框的获取焦点事件
    async showTreeData () {
      this.showTree = true
      // 获取部门列表
      let res = await getDepartments()
      // 绑定数据
      let list = res.data.depts
      // 通过之前封装好的递归函数 将数据处理成树状结构
      this.treeData = transToTreeData(list, '')
    },
    // 部门列表的点击事件
    selectNode (node) {
      // 修改输入框的内容
      this.addForm.departmentName = node.name
      // 隐藏树形结构
      this.showTree = false
    },
    // 部门输入框的失去焦点事件
    OnBlur (e) {
      // console.log(e)
      if (
        !e.relatedTarget ||
        e.relatedTarget.className.indexOf('el-tree-node') == -1
      ) {
        this.showTree = false
      }
    },
    // 点击了确定按钮
    isOk () {
      this.$refs.addForm.validate(async v => {
        // 全部通过验证
        try {
          let res = await addEmp(this.addForm)
          // console.log(res)
          this.$message.success(res.message)
          // 调用父组件里获取员工列表的函数
          // 如何得到最后一页？ 用总数量 / 页容量 再向上取整
          // 例如：10条数据，一页10条，就1页， 12条数据，一页10条两页
          let pageInfo = this.$parent.pageInfo
          pageInfo.total += 1
          let lastPage = Math.ceil(pageInfo.total / pageInfo.pagesize)
          // 让父组件请求最后一页
          this.$parent.pageInfo.page = lastPage
          this.$parent.getEmpList()
          // 因为不能直接修改父组件传过来的值 所以这里通过父组件 隐藏对话框
          this.$emit('update:showDialog', false)
        } catch (error) {
          // 没有通过验证
          console.log(error)
        }
      })
    },
    // 点击了取消按钮
    cancel () {
      // 清空表单数据
      this.addForm = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 清除校验
      this.$refs.addForm.resetFields()
      //通知父组件 隐藏对话框
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style></style>
